<html>
<head><title>Nicofarre test</title></head>
<body style="margin: 0; cursor: none">
<script src="../tma.js"></script>
<script src="mv_common.js"></script>
<script>
var init = false;
var s;
var t = 0;
var img = new Image();
img.ready = false;
img.onload = function () {
  img.ready = true;
};
img.src = 'data/jupiter.jpg';

var draw = function (api) {
  if (!init && img.ready) {
    init = true;
    s = TmaModelPrimitives.createSphere(
        4, TmaModelPrimitives.SPHERE_METHOD_EVEN);
    // We can pass the |img| directly to api.createTexture(), but once it is
    // converted to ImageData, we can modify it arbitorary.
    var imageData = api.screen.convertImage(img);
    console.log(imageData);
    for (var i = 0; i < imageData.width * imageData.height; ++i) {
      // imageData.data[i * 4 + 0] = 0;
      // imageData.data[i * 4 + 1] = 0;
      imageData.data[i * 4 + 2] *= 2;
      //imageData.data[i * 4 + 3] = 255;
    }
    s.setTexture(api.createTexture(imageData, true, Tma3DScreen.FILTER_LINEAR));
  }
  api.color = [0.0, 0.0, 0.0, 1.0];
  api.clear(api.gl.COLOR_BUFFER_BIT | api.gl.DEPTH_BUFFER_BIT);
  var p = [2000 * Math.cos(t), 0, 2000 * Math.sin(t)];
  t += 0.005;
  api.drawPrimitive(s, 200, 200, 200, p, [Math.PI / 2.0, 0.0, t * 3]);
};

tma.extlibs = [ 'ext/gl-matrix.js', 'ext/mv/MajVj.js' ];
tma.onload = function () {
  var r = 1;
  var width = 1920 / r;
  var height = 1080 / r;
  var vj = new MajVj(width, height, true);
  MajVj.loadAllPlugins().then(_(function () {
    var ceil = vj.create('frame', 'nicofarre',
                        { led: MajVj.frame.nicofarre.LED_CEILING,
                          frames: [
                            { name: 'sandbox', options: { id: '19336.0' } }
                          ]});
    var wall = vj.create('frame', 'nicofarre3d', { draw: draw });
    var fx = vj.create('effect', 'nicofarre', { front: true,
                                                position: [0, 0, 0]});
    fx.setController(controller);
    var fbo = vj.screen().createFrameBuffer(width, height);
    vj.screen().setAlphaMode(false);
    vj.run(function (delta) {
      if (emulate)
        fbo.bind();
      vj.screen().fillColor(0.0, 0.0, 0.0, 1.0);
      ceil.draw(delta);
      wall.draw(delta);
      if (emulate) {
        vj.screen().bind();
        vj.screen().fillColor(0.0, 0.0, 0.0, 1.0);
        fx.draw(delta, fbo.texture);
      }
      vj.screen().flush();
    });
    window.onresize = function () {
      vj.onresize();
      fx.onresize(vj.aspect());
    };
  }), function (e) { console.log('plugin load error: ' + e); });
};
</script>
</body>
</html>
